package com.userfactssample.client.client.screens.navigator;

import java.util.List;

import com.userfactssample.client.client.screens.UMScreen;

public class UMNavigator extends AbstractUMNavigator {
	
	int currentIndex;
	protected List<UMScreen> screens;
	
	public void attach(List<UMScreen> screens){
		attach(screens, 0);
	}
	
	public void attach(List<UMScreen> screens, int initialScreenIndex){
		this.screens = screens;
		currentIndex = initialScreenIndex;
		navigateTo(initialScreenIndex);
	}
	
	public void navigateTo(int index){
		mainScreenLayout.clear();
		screens.get(index).buildMainScreen(mainScreenLayout);
		refreshButtons();
	}
	
	@Override
	public void navigateToNext(){
		mainScreenLayout.clear();
		screens.get(++currentIndex).buildMainScreen(mainScreenLayout);
		refreshButtons();
	}
	
	@Override
	public void navigateToPrevious(){
		mainScreenLayout.clear();
		screens.get(--currentIndex).buildMainScreen(mainScreenLayout);
		refreshButtons();
	}
	
	public void refreshButtons(){
		nextButton.setTitle(screens.get(currentIndex).getNextButtonText());
		backButton.setTitle(screens.get(currentIndex).getBackButtonText());
		
		if(currentIndex == screens.size() - 1 )
			nextButton.disable();
		else
			nextButton.enable();
		
		if(currentIndex == 0 )
			backButton.disable();
		else
			backButton.enable();
		
	}
}
